package com.api.common.config;

import com.alibaba.fastjson2.JSONObject;
import com.api.common.domain.model.RuleLatestDataQuery;
import io.netty.util.internal.StringUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

/**
 * 读取项目相关配置
 *
 * @author
 */
@Component
@ConfigurationProperties(prefix = "api")
public class ApiConfig {
    /**
     * 上传路径
     */
//    private static String profile;
    /**
     * 获取地址开关
     */
    private static boolean addressEnabled;
    /**
     * 验证码类型
     */
    private static String captchaType;
    /**
     * 项目名称
     */
    private String name;
    private static String csId;
    /**
     * 版本
     */
    private String version;
    /**
     * 版权年份
     */
    private String copyrightYear;
    /**
     * 租户ID
     */
    private static String tenant;

    private static String projectPath;

    private static String charsetName;
    /**
     * 默认语种
     */
    private static String initLang;
    /**
     * 密钥key
     */
    private static String loginKey ;
    /**
     * 偏移值
     */
    private static String offsetVal;
    public static String getLoginKey() {
        return loginKey;
    }

    public static void setLoginKey(String loginKey) {
        ApiConfig.loginKey = loginKey;
    }
    public static String getOffsetVal(){
        return offsetVal;
    }
    public static void setOffsetVal(String offsetVal){
        ApiConfig.offsetVal = offsetVal;
    }

    //    private static Map<String, RuleItemNewsQuery> dataNews;
    private static Map<String, RuleLatestDataQuery> ruleCache;

    public static Map<String, RuleLatestDataQuery> getRuleCache() {
        return ruleCache;
    }

    public static void setRuleCache(Map<String, RuleLatestDataQuery> mapJo) {
        ruleCache = mapJo;
    }

    public static void changeDataNews(String key, RuleLatestDataQuery jsonValue) {
        ruleCache.replace(key, jsonValue);
    }

    public static String getInitLang() {
        return initLang;
    }

    public static String getCharsetName() {
        return charsetName;
    }

    public static String getProfile() {
        return projectPath + "/uploadPath";
    }

    public static boolean isAddressEnabled() {
        return addressEnabled;
    }

    public void setAddressEnabled(boolean addressEnabled) {
        ApiConfig.addressEnabled = addressEnabled;
    }


    /**
     * 获取导入上传路径
     */
    public static String getImportPath() {
        return getProfile() + "/import";
    }

    /**
     * 获取头像上传路径
     */
    public static String getAvatarPath() {
        return getProfile() + "/avatar";
    }

    /**
     * 获取下载路径
     */
    public static String getDownloadPath() {
        return getProfile() + "/download/";
    }

    /**
     * 获取上传路径
     */
    public static String getUploadPath() {
        return getProfile() + "/upload";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static String getCaptchaType() {
        return captchaType;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getCopyrightYear() {
        return copyrightYear;
    }

    public void setCopyrightYear(String copyrightYear) {
        this.copyrightYear = copyrightYear;
    }

    public void setCaptchaType(String captchaType) {
        ApiConfig.captchaType = captchaType;
    }

    public static String getTenant() {
        String strTenant = "00" + tenant;
        strTenant = tenant.length() < 3 ? strTenant.substring(strTenant.length() - 3) : tenant;
        return strTenant;
    }

    public void setTenant(String tenant) {
        String strTenant = "00" + tenant;
        strTenant = tenant.length() < 3 ? strTenant.substring(strTenant.length() - 3) : tenant;
        this.tenant = strTenant;
    }

    public String getProjectPath() {
        return projectPath;
    }

    public void setProjectPath(String projectPath) {
        this.projectPath = projectPath;
    }

    public static String getCsId() {
        return csId;
    }

    public void setCsId(String csId) {
        this.csId = csId;
    }

    public void setCharsetName(String charsetName) {
        this.charsetName = charsetName;
    }

    public void setInitLang(String initLang) {
        this.initLang = initLang;
    }

    /**
     * BiFunction< 实际值， 目标值（设定），返回类型>
     */
    private static final Map<String, BiFunction<Double, Double, Boolean>> operatorDoubMap = new HashMap<>();
    /**
     * BiFunction< 实际值， 目标值（设定），返回类型>
     */
    private static final Map<String, BiFunction<String, String, Boolean>> operatorStrMap = new HashMap<>();

    static {
        operatorDoubMap.put("gt", (a, b) -> a > b);
        operatorDoubMap.put("gte", (a, b) -> a >= (b)); // 非相等，即 >= 变成 !=
        operatorDoubMap.put("lt", (a, b) -> a < b);
        operatorDoubMap.put("lte", (a, b) -> a <= b);
        operatorDoubMap.put("eq", Double::equals);
        operatorDoubMap.put("neq", (a, b) -> !a.equals(b));
        operatorStrMap.put("lk", String::contains);
        operatorStrMap.put("nk", (a, b) -> !a.contains(b));
    }


    /**
     * 判断字符类
     *
     * @param operator 比较类型
     * @param param1   实际值
     * @param param2   判断值
     * @return 判读结果 true\false
     */
    public static Boolean judgeStr(String operator, String param1, String param2) {
        if (StringUtil.isNullOrEmpty(operator)) return false;
        BiFunction<String, String, Boolean> operatorFun = operatorStrMap.get(operator);
        return operatorFun.apply(param1, param2);
    }

    /**
     * @param operator 比较类型
     * @param param1   实际值
     * @param param2   判断值
     * @return 判断结果加值
     */
    public static JSONObject judgeStr2(String operator, String param1, String param2) {
        JSONObject jo = new JSONObject();
        jo.put("key", operator);
        if (StringUtil.isNullOrEmpty(operator)) {
            jo.put("flag", false);
            return jo;
        }
        ;
        BiFunction<String, String, Boolean> operatorFun = operatorStrMap.get(operator);
        jo.put("flag", operatorFun.apply(param1, param2));
        return jo;
    }

    public static Boolean judgeDouble(String operator, Double param1, Double param2) {
        if (StringUtil.isNullOrEmpty(operator)) return false;
        BiFunction<Double, Double, Boolean> operatorFun = operatorDoubMap.get(operator);
        return operatorFun.apply(param1, param2);
    }

}
